//
//  CompetitionViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/19.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CompetitionViewController.h"
#import "ContainerViewController.h"
#import "YYInfiniteLoopView.h"
#import "LearnSkillMiddleCell.h"
#import "ListenStoryHeaderCell.h"
#import "OrganationSeekCell.h"
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "OrganizationViewController.h"
@interface CompetitionViewController ()<YYInfiniteLoopViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,NewPagedFlowViewDataSource,NewPagedFlowViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)NSMutableArray *imageArra;
@end

@implementation CompetitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我赛";
    for (int index = 1; index < 5; index++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",index]];
        [self.imageArra addObject:image];
    }
    [self createCollectionUI];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark -图片轮播
- (void)loopViewDelegateAndAddToFatherView:(UIView *)faterView{
    NSArray *array = @[@"1.jpg",@"2.jpg",@"未标题-2.jpg",@"4.jpg",];
    // 类方法
    YYInfiniteLoopView *loopView = [YYInfiniteLoopView
                                    infiniteLoopViewWithImageUrls:array
                                    titles:nil
                                    didSelectedImage:^(NSInteger index) {
                                        NSLog(@"%zd",index);
                                    }];
    // 设置代理
    loopView.delegate = self;
    loopView.timeInterval = 1;
    loopView.currentPageIndicatorColor = [UIColor yellowColor];
    loopView.pagePosition = InfiniteLoopViewPagePositionCenter;
    loopView.frame = CGRectMake(0, 0, WIDTH, scaleTopAndBottom(175));
    [faterView addSubview:loopView];
}
#pragma mark - YYInfiniteLoopViewDelegate
- (void)infiniteLoopView:(YYInfiniteLoopView *)infiniteLoopView
        didSelectedImage:(NSInteger)selectedImageIndex {
    [self didSelectedImageWithIndex:selectedImageIndex];
}

- (void)didSelectedImageWithIndex:(NSInteger)index {
    NSLog(@"点击了%ld",index);
}
#pragma mark -创建collectionView
-(void)createCollectionUI{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeadertwo"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter"];
     UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
     [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
    UINib *nib3 = [UINib nibWithNibName:@"LearnSkillMiddleCell" bundle:nil];
    [self.collectionView registerNib:nib3 forCellWithReuseIdentifier:@"LearnSkillMiddleCell"];
    [self.collectionView registerClass:[OrganationSeekCell class] forCellWithReuseIdentifier:@"OrganationSeekCell"];
    [self.collectionView reloadData];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 1;
    }
    return 10;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ListenStoryHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryHeaderCell" forIndexPath:indexPath];
        [self loopViewDelegateAndAddToFatherView:cell.contentView];
        return cell;
    }else if (indexPath.section == 1){
        LearnSkillMiddleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LearnSkillMiddleCell" forIndexPath:indexPath];
       [self setupUI:cell.contentView];
        return cell;
    }else{
        OrganationSeekCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrganationSeekCell" forIndexPath:indexPath];
        return cell;
    }

}
#pragma mark - 注册collectionView的头部视图
//collectionview的头部视图的设置
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            return nil;
        }else if (indexPath.section == 1) {
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            view.backgroundColor = [UIColor whiteColor];
            [self addHeaderViewContent:view AndChineseTitle:@"最新赛事" AndEnglishTitle:@"ZuiXinSaiShi"];
            return view;
            
        }else{
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeadertwo" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            [self addHeaderViewContent:view AndChineseTitle:@"全国儿童机构搜索" AndEnglishTitle:@"The latest curriculum"];
            return view;
        }
  
    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter" forIndexPath:indexPath];
        view.backgroundColor = [UIColor cyanColor];
        return view;
    }
    
}
//设置头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(50));
}
//设置尾视图
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return CGSizeZero;
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(5));
}
//设置item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.section == 0) {
            return CGSizeMake(WIDTH, scaleTopAndBottom(175));
        }else if (indexPath.section == 1){
            return CGSizeMake(WIDTH, scaleTopAndBottom(200));
        }else{
            return CGSizeMake(scaleLeftAndRight(70), scaleTopAndBottom(100));
        }
}
//设置collectionview中的item的行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
        if (section == 0) {
            return 0;
        }
        return scaleTopAndBottom(1);
}
//设置collectionview中的item的列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return scaleLeftAndRight(1);
}
#pragma mark -选中item的状态
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        OrganizationViewController *orgaVc = [[OrganizationViewController alloc] init];
        [self.navigationController pushViewController:orgaVc animated:YES];
    }
}
#pragma mark -头部视图
-(void)addHeaderViewContent:(UIView *)titleView AndChineseTitle:(NSString *)chineTile AndEnglishTitle:(NSString *)engTitle{
    UILabel *label = [[UILabel alloc] init];
    label.text = chineTile;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView.mas_centerX);
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.width.mas_equalTo(scaleLeftAndRight(80));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView.mas_left).offset(scaleLeftAndRight(80));
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        make.right.equalTo(label.mas_left).offset(scaleLeftAndRight(-5));
    }];
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleView.mas_right).offset(scaleLeftAndRight(-80));
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        make.left.equalTo(label.mas_right).offset(scaleLeftAndRight(5));
    }];
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = engTitle;
    bottomLabel.adjustsFontSizeToFitWidth = YES;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(titleView.mas_left);
    }];
    
}

#pragma mark -创建学本领中间的图片立体手动轮播
- (void)setupUI:(UIView *)view{
    //设置高度 WIDTH * 9 / 16
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, scaleTopAndBottom(-20), WIDTH, scaleTopAndBottom(150))];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.1;
    pageFlowView.isCarousel = NO;
    pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    pageFlowView.isOpenAutoScroll = YES;
    
    //初始化pageControl
    /****************************
     使用导航控制器(UINavigationController)
     如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
     请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
     *****************************/
    UIScrollView *bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, scaleTopAndBottom(200))];
    [bottomScrollView addSubview:pageFlowView];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, scaleTopAndBottom(130), WIDTH, scaleTopAndBottom(70))];
    [self createSubviewOfView:bottomView];
    [bottomScrollView addSubview:bottomView];
    [pageFlowView reloadData];
    [view addSubview:bottomScrollView];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(WIDTH -scaleLeftAndRight(60), scaleTopAndBottom(130));
}
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    NSLog(@"点击了第%ld",(long)subIndex);
    ContainerViewController *containvc = [[ContainerViewController alloc] init];
    [self.navigationController pushViewController:containvc animated:YES];
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    return self.imageArra.count;
}
- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = (PGIndexBannerSubiew *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] initWithFrame:CGRectMake(0, 0,WIDTH - scaleLeftAndRight(60), scaleTopAndBottom(130))];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
    }
    bannerView.mainImageView.image = self.imageArra[index];
    //在这里下载网络图片
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
}
-(void)createSubviewOfView:(UIView *)view{
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"2018王者荣耀超级杯总决赛";
    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.left.and.right.equalTo(view);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.text = @"倒计时:1天1时1分";
    [view addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.left.equalTo(view.mas_left).offset(scaleLeftAndRight(60));
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.width.mas_equalTo(scaleLeftAndRight(130));
    }];
    UILabel *peopleCountLabel = [[UILabel alloc] init];
    peopleCountLabel.text = @"报名人数:2742";
    [view addSubview:peopleCountLabel];
    [peopleCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.left.equalTo(timeLabel.mas_right);
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.right.equalTo(view.mas_right);
    }];
}
#pragma mark -数组懒加载
-(NSMutableArray *)imageArra{
    if (_imageArra == nil) {
        _imageArra = [[NSMutableArray alloc] init];
    }
    return _imageArra;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
